import Vue from 'vue'
import VueRouter from 'vue-router'
import Utils from "../utils";


Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "Login" */ '@view/User/Login.vue'),
    meta: {
      isLogin: false,
      title:'登录'
    }
  },
  {
    path: '/',
    name: 'layout',
    redirect:'/login',
    component: () => import(/* webpackChunkName: "Layout" */ '@view/Layout.vue'),
    children: [
      {
        path:'/operatingCenter',
        name:'operatingCenter',
        component: resolve => require(['../views/Operation/operatingCenter.vue'], resolve),
      },

      {
        path:'/personal',
        name:'personal',
        component: resolve => require(['../views/User/personal.vue'], resolve),
      },
      
      {
        path:'/404',
        name:'404',
        component:() => import('@view/NoFound/index.vue')
      },
    ]
  },
 
  
  
  //解决
  {
    path: '*',
    redirect: '/404'
  }
];


const router = new VueRouter({
  mode: 'hash',
  routes
})

//登录过后生成菜单否则没有
if (localStorage.getItem("qx") !== null) {
  //因为每次页面跳转时都会生成一个路由,并会覆盖上一次的路由
  //每次刷新页面的时候刷新一次router
  router.addRoutes(Utils.CreatedRouter())
}

//beforeEach拦截所有页面
router.beforeEach((to, from, next) => {
  //to.meta.isLogin为true需要登录
  if (to.meta.isLogin) {
    if (localStorage.getItem("qx") !== null) {
      next()
    } else {
      next({
        path: '/login'
      })
    }
  } else {
    next();
  }
});


// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

export default router
